package com.lxblyw.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lxblyw.entity.Employee;
import com.lxblyw.util.DBHelp;
import com.lxblyw.util.RowMapper;

public class EmployeeDao {
	private DBHelp<Employee> db = new DBHelp<Employee>();
	
	public void save(Employee employee) {
		String sql = "INSERT INTO t_employee(id,`name`,`password`,createtime,pic) VALUES(?,?,?,?,?)";
		db.executeSQL(sql, employee.getId(),employee.getName(),employee.getPassword(),employee.getCreatetime(),employee.getPic());
	}
	
	public Employee findById(String id){
		String sql = "select id,`name`,`password`,createtime,pic from t_employee where id = ?";
		return db.executeQueryForObject(sql, new EmployeeRowMapper(), id);
	}
	
	private class EmployeeRowMapper implements RowMapper<Employee> {

		public Employee mapperRow(ResultSet rs) throws SQLException {
			Employee employee = new Employee();
			employee.setId(rs.getString("id"));
			employee.setName(rs.getString("name"));
			employee.setCreatetime(rs.getString("createtime"));
			employee.setPic(rs.getString("pic"));
			employee.setPassword(rs.getString("password"));
			return employee;
		}

		
	}

	public List<Employee> findByName(String username) {
		
		String sql = "select id,`name`,`password`,createtime,pic from t_employee where `name` = ? ORDER BY createtime";
		
		return db.executeQueryForList(sql, new EmployeeRowMapper(), username);
	}
	
	public List<Employee> findAllEmployees() {
		String sql = "select id,`name`,`password`,createtime,pic from t_employee order by createtime";
		
		return db.executeQueryForList(sql, new EmployeeRowMapper());
	}
	
	
	public List<Employee> findByProjectId(String id) {
		String sql = "select * from t_employee where id in (select employeeid from t_employee_project where projectid = ?)";
		return db.executeQueryForList(sql, new EmployeeRowMapper(), id);
	}
}
